-------------------------------------------------------------------------------
--  The Legend of Zelda : Oracle of Secrets
--  Copyright (C) 2010 Pixelua
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------

title_screen_img = Image.load("img/Interface/titlescreen.png", VRAM)
select_fichier_img = Image.load("img/Interface/select_fichier.png", VRAM)
select_fichier_cursor = Image.load("img/Interface/seed_cursor.png", VRAM)
press_start_img = Image.load("img/Interface/press_start.png", VRAM)
select_fichier_heart = Sprite.new("img/Interface/hearts.png", 8,7, VRAM)

select_fichier_link = Sprite.new("img/Interface/link.png", 16, 16, VRAM)
select_fichier_link:addAnimation({1,2}, 200)

press_start_timer = Timer.new()
press_start_timer:start()
title_screen_phases = 1

save01 = INI.load("saves/save01.ini", VRAM)
save02 = INI.load("saves/save02.ini", VRAM)
save03 = INI.load("saves/save03.ini", VRAM)
save = INI.load("saves/save.ini", VRAM)
pos = 1

while title_screen_phases ~= 3 do
	Controls.read()
	
	if title_screen_phases == 1 then -- Ecran Press Start --
		screen.blit(SCREEN_DOWN, 0,0,title_screen_img)
		if press_start_timer:time() > 500 then
			screen.blit(SCREEN_DOWN, 85,152,press_start_img)
			if press_start_timer:time() > 1000 then
				press_start_timer:reset()
				press_start_timer:start()
			end
		end
		if Keys.newPress.Start then title_screen_phases = 2 end
	elseif title_screen_phases == 2 then -- Ecran Choix de sauvegarde --
		screen.blit(SCREEN_UP, 0,0,title_screen_img)
		screen.blit(SCREEN_DOWN, 0,0,select_fichier_img)
		
		screen.blit(SCREEN_DOWN, 8,64+((pos-1)*32),select_fichier_cursor)
		
		if Keys.newPress.A then title_screen_phases = 3 end
		
		-- Curseur --
		if Keys.newPress.Down then 
			if pos == 3 then
				pos = 1
			else
				pos = pos + 1
			end
		elseif Keys.newPress.Up then 
			if pos == 1 then
				pos = 3
			else
				pos = pos - 1
			end
		end
		
		-- Info's --
		screen_print(SCREEN_DOWN, 23, 60,save01["save"]["save_name"],white)
		screen_print(SCREEN_DOWN, 23, 92,save02["save"]["save_name"],white)
		screen_print(SCREEN_DOWN, 23, 124,save03["save"]["save_name"],white)
		if pos == 1 then
			if save01["save"]["active"] == "true" then
				select_fichier_link:playAnimation(SCREEN_DOWN,138,74,1)
				for i=1, tonumber(save01["save"]["maxlife"]) do
					if i < 12 then
						select_fichier_heart:drawFrame(SCREEN_DOWN,130+8*i,92,0)
					else
						select_fichier_heart:drawFrame(SCREEN_DOWN,130+8*(i-11),100,0)
					end
				end
				screen.drawCounter (SCREEN_DOWN, 194, 84 ,tonumber(save01["save"]["death"]),3)
			else
				select_fichier_link:drawFrame(SCREEN_DOWN,138,74,0)
			end
		elseif pos == 2 then
			if save02["save"]["active"] == "true" then
				select_fichier_link:playAnimation(SCREEN_DOWN,138,74,1)
				for i=1, tonumber(save02["save"]["maxlife"]) do
					if i < 12 then
						select_fichier_heart:drawFrame(SCREEN_DOWN,130+8*i,92,0)
					else
						select_fichier_heart:drawFrame(SCREEN_DOWN,130+8*(i-11),100,0)
					end
				end
				screen.drawCounter (SCREEN_DOWN, 194, 84 ,tonumber(save02["save"]["death"]),3)	
			else
				select_fichier_link:drawFrame(SCREEN_DOWN,138,74,0)
			end
		else
			if save03["save"]["active"] == "true" then
				select_fichier_link:playAnimation(SCREEN_DOWN,138,74,1)
				for i=1, tonumber(save03["save"]["maxlife"]) do
					if i < 12 then
						select_fichier_heart:drawFrame(SCREEN_DOWN,130+8*i,92,0)
					else
						select_fichier_heart:drawFrame(SCREEN_DOWN,130+8*(i-11),100,0)
					end
				end
				screen.drawCounter (SCREEN_DOWN, 194, 84 ,tonumber(save03["save"]["death"]),3)
			else
				select_fichier_link:drawFrame(SCREEN_DOWN,138,74,0)
			end
		end
	end	
	render()
end

if pos == 1 then 
	save = save01 
	save01["save"]["active"] = tostring("true")
	save["save"]["save_number"] = "1"
	save["save"]["active"] = tostring("true")
elseif pos == 2 then
	save = save02 
	save02["save"]["active"] = tostring("true")
	save["save"]["save_number"] = "2"
	save["save"]["active"] = tostring("true")
else 
	save = save03 
	save03["save"]["active"] = tostring("true")
	save["save"]["save_number"] = "3"
	save["save"]["active"] = tostring("true")
end
INI.save("saves/save.ini", save)

Image.destroy(title_screen_img)
title_screen_img = nil
Image.destroy(select_fichier_img)
select_fichier_img = nil
Image.destroy(select_fichier_cursor)
select_fichier_cursor = nil
Image.destroy(press_start_img)
press_start_img = nil
select_fichier_link:destroy()
select_fichier_link = nil
select_fichier_heart:destroy()
select_fichier_heart = nil

i = nil
title_screen_phases = nil
nb_death = nil
pos = nil
collectgarbage()
-- Transition --

tmptmr = Timer.new()
tmptmr:start()
while not tmptmr:time() == 1500 do
	screen.drawFillRect(SCREEN_UP, 0, 0, 256, 192, Color.new(0, 0, 0))
	screen.drawFillRect(SCREEN_DOWN, 0, 0, 256, 192, Color.new(0, 0, 0))
end
tmptmr = nil

dofile("map_manager.lua")
